using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

[System.ComponentModel.DesignerCategory("Code")]
public class GUI2 : Form, MyToolStripParent
{
    BrowserControl userControl11;
    MyMenuStrip menuStrip1;
    MyToolStrip toolStrip1;
    IContainer components = null;

    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new GUI2());
    }

    private class MyToolStrip : ToolStrip
    {
        MyToolStripParent parent;
        public ToolStripButton toolStripButton1;
        public ToolStripButton toolStripButton2;
        public ToolStripButton toolStripButton3;
        public ToolStripButton Delete;

        private class ButtonAdd : ToolStripButton
        {
            public ButtonAdd() : base()
            {
                DisplayStyle = ToolStripItemDisplayStyle.Image;
                ImageTransparentColor = Color.Magenta;
                Name = "toolStripButton1";
                Size = new Size(23, 22);
                Text = "Add";
            }
        }

        private class ButtonExtract : ToolStripButton
        {
            public ButtonExtract()
            {
                DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
                ImageTransparentColor = System.Drawing.Color.Magenta;
                Name = "toolStripButton2";
                Size = new System.Drawing.Size(23, 22);
                Text = "Extract";
            }
        }

        public MyToolStrip(MyToolStripParent parent)
        {
            this.parent = parent;
            ComponentResourceManager resources = new ComponentResourceManager(typeof(GUI2));
            Dock = DockStyle.Top;
            BringToFront();
            Location = new Point(0, 24);
            Name = "toolStrip1";
            Size = new Size(424, 25);
            TabIndex = 2;
            Text = "toolStrip1";
            toolStripButton1 = new ButtonAdd();
            toolStripButton2 = new ToolStripButton();
            toolStripButton3 = new ToolStripButton();
            Delete = new ToolStripButton();
            toolStripButton1.Image = ((Image)(resources.GetObject("toolStripButton1.Image")));
            toolStripButton1.Click += new EventHandler(parent.add);
            toolStripButton2.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton2.Image")));
            toolStripButton2.Click += new System.EventHandler(parent.extract);
            toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
            toolStripButton3.Image = ((Image)(resources.GetObject("toolStripButton3.Image")));
            toolStripButton3.ImageTransparentColor = Color.Magenta;
            toolStripButton3.Name = "toolStripButton3";
            toolStripButton3.Size = new System.Drawing.Size(23, 22);
            toolStripButton3.Text = "Test";
            toolStripButton3.Click += new EventHandler(parent.toolStripButtonTest_Click);
            Delete.DisplayStyle = ToolStripItemDisplayStyle.Image;
            Delete.Image = ((System.Drawing.Image)(resources.GetObject("Delete.Image")));
            Delete.ImageTransparentColor = System.Drawing.Color.Magenta;
            Delete.Name = "Delete";
            Delete.Size = new Size(23, 22);
            Delete.Text = "Delete";
            Delete.Click += new System.EventHandler(parent.delete);
            Items.AddRange(new ToolStripItem[] { toolStripButton1, toolStripButton2, toolStripButton3, Delete });
            SuspendLayout();
        }
    }

    public GUI2()
    {
        InitializeComponent();
        userControl11.chdir(new DirectoryInfo(Directory.GetCurrentDirectory()));
    }

    public void quitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        Close();
    }

    public void add(object sender, EventArgs e)
    {
        CreatorDialog dlg = new CreatorDialog();
        dlg.setFileList(userControl11.getSelectedFiles());
        dlg.ShowDialog();

        if (dlg.spinnerThreads.Value == 7)
            Console.Error.WriteLine("geluksgetal");

        if (dlg.btnOK.Clicked)
        {
            CreatingDialog progressDlg = new CreatingDialog((int)dlg.spinnerThreads.Value);
            progressDlg.files = dlg.files;
            progressDlg.targetName = dlg.comboBox1.Text;
            progressDlg.compressie = dlg.comboBox2.Text;
            progressDlg.multiProc = dlg.radioButton2.Checked;
            progressDlg.ShowDialog();
        }

        userControl11.refresh();
    }

    private void userControl11_Load(object sender, EventArgs e)
    {
        userControl11.refresh();
    }

    private void GUI2_Load(object sender, EventArgs e)
    {
        userControl11.chdir(new DirectoryInfo(Directory.GetCurrentDirectory()));
        userControl11.refresh();
    }

    public void refreshToolStripMenuItem_Click(object sender, EventArgs e)
    {
        userControl11.refresh();
    }

    public void extract(object sender, EventArgs e)
    {
        MyList list = userControl11.getSelectedFiles();
        BrowserState state = userControl11.getState();

        if (state.GetType().Equals(typeof(ParItState)))
        {
            ExtractDialog dlg = new ExtractDialog(userControl11.parItFile, userControl11.getPWD());
            dlg.Show();
        }
        else if (list[0].Name.EndsWith(".prt"))
        {
            ArchiveFactory archiveFactory = new ArchiveFactory();
            Archive archief = archiveFactory.load(list[0].FullName);
            ExtractDialog dlg = new ExtractDialog(archief, userControl11.getPWD());
            dlg.Show();
        }
    }

    public void toolStripButtonTest_Click(object sender, EventArgs e)
    {
        MyList list = userControl11.getSelectedFiles();

        if (userControl11.parItFile != null)
        {
            TestingDialog dlg = new TestingDialog(userControl11.parItFile);
            dlg.ShowDialog();
        }
        else if (list[0].Name.EndsWith(".prt"))
        {
            ArchiveFactory archiveFactory = new ArchiveFactory();
            using (Archive archief = archiveFactory.load(list[0].FullName))
            {
                TestingDialog dlg = new TestingDialog(archief);
                dlg.ShowDialog();
            }
        }
    }

    public void delete(object sender = null, EventArgs e = null)
    {
        MyList lijst = userControl11.getSelectedFiles();
        lijst.delete();
        userControl11.refresh();
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        bool bHandled = false;

        switch (keyData)
        {
            case Keys.Delete:
                delete();
                break;
            case Keys.F5:
                userControl11.refresh();
                bHandled = true;
                break;
        }

        return bHandled;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
            components.Dispose();

        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        ComponentResourceManager resources = new ComponentResourceManager(typeof(GUI2));
        menuStrip1 = new MyMenuStrip(this);
        userControl11 = new BrowserControl();
        menuStrip1.SuspendLayout();
        toolStrip1 = new MyToolStrip(this);
        userControl11.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
        userControl11.Location = new System.Drawing.Point(1, 52);
        userControl11.Name = "userControl11";
        userControl11.Size = new System.Drawing.Size(630, 400);
        userControl11.TabIndex = 0;
        userControl11.Load += new EventHandler(userControl11_Load);
        userControl11.ExtractClicked += new EventHandler(extract);
        AutoScaleDimensions = new SizeF(6F, 13F);
        AutoScaleMode = AutoScaleMode.Font;
        ClientSize = new System.Drawing.Size(635, 328);
        SuspendLayout();
        Controls.Add(toolStrip1);
        Controls.Add(this.userControl11);
        Controls.Add(this.menuStrip1);
        MainMenuStrip = this.menuStrip1;
        Name = "GUI2";
        Text = "GUI2";
        Size = new System.Drawing.Size(640, 480);
        Load += new System.EventHandler(this.GUI2_Load);
        menuStrip1.ResumeLayout(false);
        menuStrip1.PerformLayout();
        toolStrip1.ResumeLayout(false);
        toolStrip1.PerformLayout();
        ResumeLayout(false);
        PerformLayout();
    }
}

public class MyMenuStrip : MenuStrip
{
    GUI2 parent;
    ToolStripMenuItem fileToolStripMenuItem;
    ToolStripMenuItem editToolStripMenuItem;
    ToolStripMenuItem quitToolStripMenuItem;
    ToolStripMenuItem refreshToolStripMenuItem;
    ToolStripMenuItem helpToolStripMenuItem;
    ToolStripMenuItem aboutToolStripMenuItem;
    ToolStripMenuItem extractToolStripMenuItem;

    public MyMenuStrip(GUI2 parent)
    {
        this.parent = parent;
        fileToolStripMenuItem = new ToolStripMenuItem();
        quitToolStripMenuItem = new ToolStripMenuItem();
        editToolStripMenuItem = new ToolStripMenuItem();
        helpToolStripMenuItem = new ToolStripMenuItem();
        refreshToolStripMenuItem = new ToolStripMenuItem();
        aboutToolStripMenuItem = new ToolStripMenuItem();
        extractToolStripMenuItem = new ToolStripMenuItem();
        Location = new Point(0, 0);
        Name = "menuStrip1";
        Size = new Size(424, 24);
        TabIndex = 1;
        Text = "menuStrip1";
        Items.AddRange(new ToolStripItem[] { fileToolStripMenuItem, editToolStripMenuItem, helpToolStripMenuItem });
        
        editToolStripMenuItem.Name = "editToolStripMenuItem";
        editToolStripMenuItem.Size = new Size(35, 20);
        editToolStripMenuItem.Text = "Edit";
        editToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { refreshToolStripMenuItem });
        extractToolStripMenuItem.Name = "extractToolStripMenuItem";
        extractToolStripMenuItem.Text = "Extract";
        extractToolStripMenuItem.Click += new EventHandler(parent.extract);
        refreshToolStripMenuItem.Name = "refreshToolStripMenuItem";
        refreshToolStripMenuItem.Size = new Size(112, 22);
        refreshToolStripMenuItem.Text = "Refresh";
        refreshToolStripMenuItem.Click += new EventHandler(parent.refreshToolStripMenuItem_Click);
        quitToolStripMenuItem.Name = "quitToolStripMenuItem";
        quitToolStripMenuItem.Size = new System.Drawing.Size(112, 22);
        quitToolStripMenuItem.Text = "Quit";
        quitToolStripMenuItem.Click += new EventHandler(parent.quitToolStripMenuItem_Click);
        helpToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { aboutToolStripMenuItem });
        helpToolStripMenuItem.Name = "helpToolStripMenuItem";
        helpToolStripMenuItem.Size = new System.Drawing.Size(40, 20);
        helpToolStripMenuItem.Text = "Help";
        editToolStripMenuItem.Name = "editToolStripMenuItem";
        aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
        aboutToolStripMenuItem.Size = new System.Drawing.Size(103, 22);
        aboutToolStripMenuItem.Text = "About";
        aboutToolStripMenuItem.Click += new EventHandler(aboutToolStripMenuItem_Click);

        fileToolStripMenuItem.DropDownItems.AddRange(new ToolStripItem[] { extractToolStripMenuItem, quitToolStripMenuItem });
        fileToolStripMenuItem.Name = "fileToolStripMenuItem";
        fileToolStripMenuItem.Size = new Size(35, 20);
        fileToolStripMenuItem.Text = "File";
    }

    private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
    {
        About aboutDlg = new About();
        aboutDlg.ShowDialog();
    }
}

public interface MyToolStripParent
{
    void extract(object sender, EventArgs e);
    void toolStripButtonTest_Click(object sender, EventArgs e);
    void add(object sender, EventArgs e);
    void delete(object sender, EventArgs e);
}

